package com.lsh.payment.core.service.alipay.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.lsh.payment.core.model.payment.PayDeal;
import com.lsh.payment.core.model.refund.PayRefund;
import com.lsh.payment.core.service.alipay.IAliRefundService;
import com.lsh.payment.core.strategy.config.AliPayConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * Project Name: lsh-payment
 * Created by peter on 18/6/1.
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service
@Slf4j
public class AliRefundServiceImpl implements IAliRefundService {

    @Override
    public AlipayTradeRefundResponse refund(PayRefund payRefund, PayDeal payDeal) {

//        String logPrefix = "【支付宝退款】";
        AlipayClient alipayClient = this.getClient();
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setOutTradeNo(payRefund.getPayPaymentNo());
        model.setTradeNo(payDeal.getChannelTransaction());
        model.setOutRequestNo(payRefund.getPaymentRefundNo());
        model.setRefundAmount(payRefund.getRefundAmount().toString());
        model.setRefundReason("正常退款");
        request.setBizModel(model);

        AlipayTradeRefundResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            log.error("", e);
        }

        return response;
    }


    private DefaultAlipayClient getClient() {

        return new DefaultAlipayClient(AliPayConfig.ALIPAY_URL, AliPayConfig.app_id, AliPayConfig.private_key, AliPayConfig.FORMAT, AliPayConfig.input_charset, AliPayConfig.public_key, AliPayConfig.sign_type_2);
    }

    @Override
    public AlipayTradeFastpayRefundQueryResponse refundQuery(PayRefund payRefund, PayDeal payDeal) {

        AlipayClient client = this.getClient();
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
        model.setOutTradeNo(payRefund.getPayPaymentNo());
        model.setTradeNo(payDeal.getChannelTransaction());
        model.setOutRequestNo(payRefund.getPaymentRefundNo());
        request.setBizModel(model);
        AlipayTradeFastpayRefundQueryResponse response = null;
        try {
            response = client.execute(request);
        } catch (AlipayApiException e) {
            log.error("", e);
        }

        return response;
    }
}
